package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Voice;
import com.tanhua.dubbo.api.mongo.VoiceApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class VoiceMqService {

    @Reference
    private VoiceApi voiceApi;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;




    /**
     * 发布语音
     * @param voiceId
     */
    public void voiceMsg(String voiceId){
        this.sendMsg(voiceId,1);
    }


    /**
     * 发送消息
     *  参数：动态id
     *  参数type：
     *     type 1-发送语音
     */
    private void sendMsg(String voiceId,Integer type){
        //根据主键查询获取vid
        Voice voice = voiceApi.findById(voiceId);
        try {
            Map<String, String> message = new HashMap<>();
            message.put("userId", UserHolder.getUserId().toString());
            message.put("voiceId",voiceId);
            message.put("type", type.toString());
            message.put("vid", voice.getVid().toString());
            rocketMQTemplate.convertAndSend("tanhua-voice-151", JSON.toJSONString(message));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发送消息失败! publishId = " + voiceId + ", type = " + type);
        }
    }



}
